package com.zx.sys.cms.api.admin;

import com.alibaba.fastjson.JSONArray;
import com.zx.common.utils.Constant;
import com.zx.common.utils.PageUtils;
import com.zx.common.utils.R;
import com.zx.common.utils.ShiroUtils;
import com.zx.sys.cms.entity.CmsContentEntity;
import com.zx.sys.cms.service.CmsContentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;



/**
 *
 *
 * @author LiYe
 * @email sunlightcs@gmail.com
 * @date 2020-03-09 16:15:28
 */
@RestController
@RequestMapping("/sys/cmscontent")
public class CmsContentApi {
    @Autowired
    private CmsContentService cmsContentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:cmscontent:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = cmsContentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:cmscontent:info")
    public R info(@PathVariable("id") Long id){
		CmsContentEntity cmsContent = cmsContentService.getById(id);

        return R.ok().put("info", cmsContent);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:cmscontent:save")
    public R save(@RequestBody CmsContentEntity cmsContent){
        Integer type = cmsContent.getType();
        Long userId = ShiroUtils.getUserId();

        cmsContent.setCreator(userId);
        cmsContent.setCreateTime(new Date());
        cmsContent.setUpdater(userId);
        cmsContent.setUpdateTime(new Date());

        if(type !=null && type.equals(Constant.CmsContentType.image.getValue())){
            JSONArray pictureArr = cmsContent.getPicturesArray();
            cmsContent.setPictures(pictureArr.toJSONString());
            if(pictureArr != null && pictureArr.size()>0){
                cmsContent.setPicture(pictureArr.getJSONObject(0).getString("url"));
            }
        }else {
            cmsContent.setPictures(null);
        }

		cmsContentService.save(cmsContent);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:cmscontent:update")
    public R update(@RequestBody CmsContentEntity cmsContent){
        Integer type = cmsContent.getType();
        Long userId = ShiroUtils.getUserId();

        cmsContent.setUpdater(userId);
        cmsContent.setUpdateTime(new Date());

        if(type != null && type.equals(Constant.CmsContentType.imageText.getValue())){
            cmsContent.setPictures(null);
            cmsContent.setMedia(null);
        } else if(type != null && type.equals(Constant.CmsContentType.image.getValue())){
            JSONArray pictureArr = cmsContent.getPicturesArray();
            if(pictureArr != null && pictureArr.size()>0){
                cmsContent.setPictures(pictureArr.toJSONString());
                cmsContent.setPicture(pictureArr.getJSONObject(0).getString("url"));
            }
            cmsContent.setContent(null);
            cmsContent.setMedia(null);
        }else {
            cmsContent.setPictures(null);
            cmsContent.setContent(null);
        }

		cmsContentService.updateById(cmsContent);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:cmscontent:delete")
    public R delete(@RequestBody Long[] ids){
		cmsContentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
